package cn.tedu.account.tcc;

import cn.tedu.account.entity.Account;
import cn.tedu.account.mapper.AccountMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
@Component
public class AccountTCCImpl implements AccountTCC {
    @Autowired
    private AccountMapper accountMapper;
    @Transactional
    @Override
    public boolean prepare(BusinessActionContext ctx, Long userId, BigDecimal money) {
        Account account = accountMapper.selectById(userId);
        if(account.getResidue().compareTo(money)<0){
            throw new RuntimeException("可用金额不足");
        }
        accountMapper.updateResidueToFrozen(userId, money);
        ResultHolder.setResult(AccountTCC.class, ctx.getXid(), "p");
        return true;
    }
   @Transactional
    @Override
    public synchronized boolean commit(BusinessActionContext ctx) {
        if(ResultHolder.getResult(AccountTCC.class, ctx.getXid())==null){
            return  true;
        }
       Long userId = Long.valueOf(ctx.getActionContext("usrId").toString());
       BigDecimal money = new BigDecimal(ctx.getActionContext("money").toString());

       accountMapper.updateFrozenToUsed(userId,money);
       ResultHolder.removeResult(AccountTCC.class, ctx.getXid());

        return true;
    }
    @Transactional
    @Override
    public synchronized boolean rollback(BusinessActionContext ctx) {
        if(ResultHolder.getResult(AccountTCC.class, ctx.getXid())==null){
            return  true;
        }
        Long userId = Long.valueOf(ctx.getActionContext("usrId").toString());
        BigDecimal money = new BigDecimal(ctx.getActionContext("money").toString());

        accountMapper.updateFrozenToResidue(userId,money);
        ResultHolder.removeResult(AccountTCC.class, ctx.getXid());

        return true;
    }
}
